 // 关联／未关联状态码
    var pageStatus = 0; //0表示当前处于已关联员工列表

    // 保存已关联员工复选框取消状态
    var cancelLinkCheck = [];
    // 保存未关联员工复选框取消状态
    var cancelUnlinkCheck = [];
    // 非全选时保存的id
    var saveIdArr = [];

$(function() {
   
    // 初始化表格
    viewTable(1);

    $(document).on('click','.premiss-checked',function(){
         $(this).toggleClass("on_check");
    })


    // 读取角色详细信息
    $(document).on('click', '.edit-role', function() {
        var thisID = $(this).children('.role-id').val();
        $('.thisID-input').val(thisID);
        console.log($('.thisID-input').val());
        $('tr').removeClass('selected');
        $(this).parents('tr').first().addClass('selected');
        var thisName = $('.selected').children('td:eq(0)').text();
        $('.role-name').val(thisName);
        var premiss = $(this).children('.permiss-val').val();
        if(premiss != undefined){
        	premiss = premiss.substring(0, premiss.length - 1);
        }else{
        	premiss = "";
        }
        
        var premissArr = premiss.split('-');
        // for(var i=0;i<premissArr.length;i++){
        //  premissArr[i] = parseInt(premissArr[i])
        // }
        console.log(thisName);
        loading();
        $.ajax({
            url: "/epi/admin/permission/permissions",
            type: "GET",
            dataType: 'json',
            success: function(data) {
            	console.log(JSON.stringify(data));
                loadingHide();
                // alert(JSON.stringify(data));
                var myTemplate = Handlebars.compile($("#premiss-template").html());
                $('.checkbox-div').html(myTemplate(data));
                for (var j = 0; j < $('.premiss-checked').length; j++) {
                    for (var k = 0; k < premissArr.length; k++) {
                        if (premissArr[k] == $('.premiss-checked').eq(j).children('.check-input').attr('name')) {
                            $('.premiss-checked').eq(j).addClass('on_check')
                        }
                        premissArr[k] = parseInt(premissArr[k])
                    }
                }
                $('.submit-add input').attr('class', 'btn btn-update-submit')
                fadeModal('.add-modal');
            },
            error: function() {
                setAlert('系统繁忙，请刷新重试');
            }
        });
        console.log($('.premiss-checked'));
        // console.log($('.premiss-checked').length);
        // $('.role-name').val(thisName);
    })

    // 删除角色
    $(document).on('click', '.delete-role', function() {
        loading();
        var thisID = $(this).children('.role-id').val();
        setConfirm('是否确认删除该角色', function() {
            $.ajax({
                url: "/epi/admin/role/" + thisID,
                type: "POST",
                data: {
                    _method: "DELETE"
                },
                traditional : true,
                dataType: 'json',
                success: function(data) {
                    loadingHide();
                    console.log(JSON.stringify(data));                
                    var status = data.status;
                    console.log(status == 200);
                    if (status == 500) {
                        setAlert('非法操作！');
                    } else if (status == 400) {
                        setAlert('删除失败！');
                    } else if (status == 200) {
                        setAlert('删除成功');
                        viewTable(1);
                    } else {
                        setAlert('系统繁忙，请刷新重试');
                    }
                },
                error: function() {
                    setAlert('系统繁忙，请刷新重试');
                }
            });
        })
    })

    // 关联员工模态框
    $(document).on('click', '.link-role', function() {
        //默认显示已关联的员工
        pageStatus = 0;
        $('.staff-type button').removeClass('active');
        $('.staff-type button').eq(0).addClass('active');
        
        var thisID = $(this).children('.role-id').val();
        $('.thisID-input').val(thisID);
        viewLinkStaff(1);
        // $('.checked').eq(2).attr('class','check staff-check on_check');
        $('.staff-checked').removeClass('on_check');
        fadeModal('.link-modal');
    })

    
    // 切换已关联和未关联员工列表
    $('.staff-type .btn').click(function() {
        var a = $('.staff-type .btn').index(this);

        saveIdArr.splice(0, saveIdArr.length); //清空存储id
        checkStatus = 0; //全选状态归0
        $('.staff-checked').removeClass('on_check');

        if (a == 0) {
            viewLinkStaff(1);
            pageStatus = 0;
        } else if (a == 1) {
            viewNotLinkStaff(1);
            pageStatus = 1;
        }
        $('.linkPage-input').val(1);
        $('.staff-type .btn').removeClass('active');
        $(this).addClass('active');
    })

    //添加角色模态框
    $('.btn-add').click(function() {
        $('.role-name')[0].value = '';
        $('.premiss-checked').removeClass('on_check');
        $('.submit-add input').attr('class', 'btn btn-add-submit')
        viewPremiss();
    })

    //确认新增角色
    $(document).on('click', '.btn-add-submit', function() {
        var roleName = $('.role-name').val();
        var checked = $('.premiss-checked.on_check');
        var Permissions = [];
        if (roleName == '') {
            setAlert('请输入角色名')
        } else {
            for (var i = 0; i < checked.length; i++) {
                var thisID = parseInt(checked.eq(i).children('.check-input').attr('name'));
                Permissions.push(thisID);
            }

            //防止测试报错
            if (isNaN(Permissions[0])) {
                Permissions = [];
                Permissions[0] = 1;
                Permissions[1] = 2;
            }
            loading();
            $.ajax({
                url: "/epi/admin/role/",
                traditional: true,
                data: {
                    name: roleName,
                    permissions: Permissions
                },
                type: "POST",
                dataType: 'json',
                success: function(data) {
                	console.log(data);
                    loadingHide();
                    // console.log(JSON.stringify(data))
                    var status = data.status;
                    if (status == 500) {
                        setAlert('非法操作！');
                    } else if (status == 400) {
                        setAlert('添加失败！');
                    } else if (status == 200) {
                        setAlert('添加成功');
                        viewTable(1);
                        $('.add-modal,.mask').fadeOut();
                    } else {
                        setAlert('系统繁忙，请刷新重试');
                    }


                },
                error: function() {
                    setAlert('系统繁忙，请刷新重试');
                }
            });
        }
    })

    // 确认更新角色
    $(document).on('click', '.btn-update-submit', function() {
        var roleName = $('.role-name').val();
        var checked = $('.premiss-checked.on_check');
        var Permissions = [];
        var PerName = '';
        if (roleName == '') {
            setAlert('请输入角色名')
        } else {
            console.log(checked.length);
            for (var i = 0; i < checked.length; i++) {
                var thisID = parseInt(checked.eq(i).children('.check-input').attr('name'));
                Permissions.push(thisID);
                PerName += checked.eq(i).next().first().text() + '  ';
            }

            //防止测试报错
            if (isNaN(Permissions[0])) {
                Permissions = [];
                Permissions[0] = 1;
                Permissions[1] = 2;
            }
            loading();
            console.log(Permissions)
            $.ajax({
                url: "/epi/admin/role/" + $('.thisID-input').val(),
                type: "POST",
                traditional: true,
                data: {
                    _method: 'PUT',
                    name: roleName,
                    Permissions: Permissions
                },
                dataType: 'json',
                success: function(data) {
                	console.log(JSON.stringify(data));
                    //alert(JSON.stringify(data))
                    loadingHide();
                    var status = data.status;
                    if (status == 500) {
                        setAlert('非法操作！');
                    } else if (status == 400) {
                        setAlert('更新失败！');
                    } else if (status == 200) {
                        setAlert('更新成功');
                        $('.selected td:eq(0)').html(roleName);
                        $('.selected td:eq(1)').html(PerName);
                        $('.add-modal,.mask').fadeOut();
                    } else {
                        setAlert('系统繁忙，请刷新重试');
                    }


                },
                error: function() {
                    setAlert('系统繁忙，请刷新重试');
                }
            });
        }
    })

    // 翻页－下一页
    $('.paging .next').click(function() {
        $('.linkPage-input')[0].value++;

        console.log("当前页码：" + $('.linkPage-input')[0].value);
        if (pageStatus == 0) {

            if (checkStatus == 1) { //全选状态下,保存取消选择的员工ID和页码对象
                viewLinkStaff($('.linkPage-input').val());
                checkState(cancelLinkCheck);
                allNextPage(cancelLinkCheck);
            } else { //非全选状态下，保存选择的员工ID和页面对象
                viewLinkStaff($('.linkPage-input').val());
                checkState(saveIdArr);
                nextPage(saveIdArr);

            }

            // saveLinkCancelId(cancelLinkCheck);
        } else if (pageStatus == 1) {

            if (checkStatus == 1) { //全选状态下,保存取消选择的员工ID和页码对象
                viewNotLinkStaff($('.linkPage-input').val());
                checkState(cancelUnlinkCheck);
                allNextPage(cancelUnlinkCheck);

            } else { //非全选状态下，保存选择的员工ID和页面对象
                viewNotLinkStaff($('.linkPage-input').val());
                checkState(saveIdArr);
                nextPage(saveIdArr);

            }
        }
    })

    // 翻页－上一页
    $('.paging .pre').click(function() {
        $('.linkPage-input')[0].value--;
        console.log("当前页码：" + $('.linkPage-input')[0].value)
        if ($('.linkPage-input')[0].value != 0) {
            if (pageStatus == 0) { //已关联
                viewLinkStaff($('.linkPage-input').val());
                if (checkStatus == 1) { //全选状态下,保存取消选择的员工ID和页码对象
                    allPrevPage(cancelLinkCheck);
                    checkState(cancelLinkCheck);
                } else if (checkStatus == 0) { //非全选状态下，保存选择的员工ID和页面对象
                    prevPage(saveIdArr);
                    checkState(saveIdArr);
                }
            } else if (pageStatus == 1) {
                viewNotLinkStaff($('.linkPage-input').val());
                if (checkStatus == 1) { //全选状态下,保存取消选择的员工ID和页码对象
                    allPrevPage(cancelUnlinkCheck);
                    checkState(cancelUnlinkCheck);
                } else if (checkStatus == 0) { //非全选状态下，保存选择的员工ID和页面对象
                    prevPage();
                    checkState(saveIdArr);
                }
            }
        } else {
            setAlert('已至第一页');
        }
    })

    // 搜索员工
    $('.search-btn').click(function() {
        if(pageStatus == 0){
            viewLinkStaff(1)
        }else if(pageStatus == 1){
            viewNotLinkStaff(1)
        }
        
    })

    // 关联／取消关联员工
    $(document).on('click', '.btn-link', function() {
    	
    	
        // alert(000)
        // $('.staff-checked').removeClass('on_check');
        var staffIdArr = [];
        // var checked = $('.staff-checked.on_check');
        // console.log(checked);
        // for (var i = 0; i < checked.length; i++) {
        //     var id = parseInt(checked.eq(i).next('.person-id').val());
        //     .push(id);
        // }
        // console.log(staffIdArr);
        console.log($('.thisID-input').val());

        console.log($('.search-input').val());
        if (pageStatus == 0) {
            if (checkStatus == 0) { //非全选
                //更新本页ID
                var checkId = [];
                console.log("全选状态：" + checkStatus);
                var check = $('.staff-checked.on_check');
                var flag = 0; //翻页前一页是否存入过数组，0表示未存过
                if (check.length != 0) {
                    for (var i = 0; i < check.length; i++) {
                        checkId.push(check.eq(i).next('.person-id').val());
                    }
                    for (var j = 0; j < saveIdArr.length; j++) {
                        if (saveIdArr[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //保存翻页前一页状态
                            saveIdArr[j].checkId = checkId; //翻页时更新取消id数组
                            flag = 1; //flag为1表示存过，此时更新
                        }
                    }
                    if (flag == 0) { //flag为0表示未存过，此时新增
                        saveIdArr.push({
                            pageNum: parseInt($('.linkPage-input')[0].value),
                            checkId: checkId
                        })
                    }
                    console.log("所存入翻页前页面和取消ID：")
                    console.log(saveIdArr);
                    for (var i1 = 0; i1 < saveIdArr.length; i1++) {
                        for (var i2 = 0; i2 < saveIdArr[i1].checkId.length; i2++) {
                            staffIdArr.push(saveIdArr[i1].checkId[i2]);
                        }
                    }
                }
            } else if (checkStatus == 1) {
                var cancelId = [];
                console.log("全选状态：" + checkStatus);
                var unCheck = $('.staff-checked').not('.on_check');
                var flag = 0; //该页是否存入过数组，0表示未存过
                if (unCheck.length != 0) {
                    for (var i = 0; i < unCheck.length; i++) {
                        cancelId.push(unCheck.eq(i).next('.person-id')[0].value);
                    }
                    for (var j = 0; j < cancelLinkCheck.length; j++) {
                        if (cancelLinkCheck[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //
                            cancelLinkCheck[j].cancelId = cancelId; //翻页时更新取消id数组
                            flag = 1; //flag为1表示存过，此时更新
                        }
                    }
                    if (flag == 0) { //flag为0表示未存过，此时新增
                        cancelLinkCheck.push({
                            pageNum: parseInt($('.linkPage-input')[0].value),
                            cancelId: cancelId
                        })
                    }
                    console.log("所存入翻页前页面和取消ID：")
                    console.log(cancelLinkCheck);
                    for (var i1 = 0; i1 < cancelLinkCheck.length; i1++) {
                        for (var i2 = 0; i2 < cancelLinkCheck[i1].cancelId.length; i2++) {
                            staffIdArr.push(cancelLinkCheck[i1].cancelId[i2]);
                        }
                    }
                }
            }
            loading();
            console.log("staffIdArr=-------------------------")
            var dataIntArr=[];
            staffIdArr.forEach(function(data,index,arr){  
		        dataIntArr.push(+data);  
		    }); 
            
            console.log(staffIdArr);
            console.log(dataIntArr);
            console.log(checkStatus);
            console.log($('.search-input').val());
            
            
            var hdids = [1,2];
            var hdkey = "";
            var hdcheckStatus = 0;
            //取消关联
            var options = getBASEDELETEAJAX();
            options.url = "/epi/admin/role/" + $('.thisID-input').val() + "/staffs"
            options.data.staffIds = dataIntArr
            options.data.key = $('.search-input').val()
            options.data.cancelIds = dataIntArr
            options.data.checkStatus = checkStatus
            
           $.ajax(options
        		/*{
                url: "/epi/admin/role/" + $('.thisID-input').val() + "/staffs",
                traditional : true,
                data: {
                    _method: "DELETE",
                	staffIds:staffIdArr,
                    checkStatus:checkStatus,//标志是否全选
                    key:$('.search-input').val(),//搜索文字
                    cancelIds:staffIdArr
                },
                type: "POST",
                dataType: 'json',
                success: function(data) {
                	console.log(JSON.stringify(data));
                    loadingHide();
                    var status = data.status;
                    if (status == 500) {
                        setAlert('非法操作！');
                    } else if (status == 400) {
                        setAlert('取消关联失败！');
                    } else if (status == 200) {
                        setAlert('取消关联成功');
                    } else {
                        setAlert('系统繁忙，请刷新重试');
                    }
                },
                error: function() {
                    setAlert('系统繁忙，请刷新重试');
                }
            }*/);
        } else if (pageStatus == 1) {
            if (checkStatus == 0) { //非全选
                //更新本页ID
                var checkId = [];
                console.log("全选状态：" + checkStatus);
                var check = $('.staff-checked.on_check');
                var flag = 0; //翻页前一页是否存入过数组，0表示未存过
                if (check.length != 0) {
                    for (var i = 0; i < check.length; i++) {
                        checkId.push(check.eq(i).next('.person-id').val());
                    }
                    for (var j = 0; j < saveIdArr.length; j++) {
                        if (saveIdArr[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //保存翻页前一页状态
                            saveIdArr[j].checkId = checkId; //翻页时更新取消id数组
                            flag = 1; //flag为1表示存过，此时更新
                        }
                    }
                    if (flag == 0) { //flag为0表示未存过，此时新增
                        saveIdArr.push({
                            pageNum: parseInt($('.linkPage-input')[0].value),
                            checkId: checkId
                        })
                    }
                    console.log("所存入翻页前页面和取消ID：")
                    console.log(saveIdArr);
                    for (var i1 = 0; i1 < saveIdArr.length; i1++) {
                        for (var i2 = 0; i2 < saveIdArr[i1].checkId.length; i2++) {
                            staffIdArr.push(saveIdArr[i1].checkId[i2]);
                        }
                    }
                }
            } else if (checkStatus == 1) {
                var cancelId = [];
                console.log("全选状态：" + checkStatus);
                var unCheck = $('.staff-checked').not('.on_check');
                var flag = 0; //该页是否存入过数组，0表示未存过
                if (unCheck.length != 0) {
                    for (var i = 0; i < unCheck.length; i++) {
                        cancelId.push(unCheck.eq(i).next('.person-id').val());
                    }
                    for (var j = 0; j < cancelUnLinkCheck.length; j++) {
                        if (cancelUnLinkCheck[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //
                            cancelUnLinkCheck[j].cancelId = cancelId; //翻页时更新取消id数组
                            flag = 1; //flag为1表示存过，此时更新
                        }
                    }
                    if (flag == 0) { //flag为0表示未存过，此时新增
                        cancelUnLinkCheck.push({
                            pageNum: parseInt($('.linkPage-input')[0].value),
                            cancelId: cancelId
                        })
                    }
                    console.log("所存入翻页前页面和取消ID：")
                    console.log(cancelUnLinkCheck);
                    for (var i1 = 0; i1 < saveIdArr.length; i1++) {
                        for (var i2 = 0; i2 < saveIdArr[i1].cancelUnLinkCheck.length; i2++) {
                            staffIdArr.push(saveIdArr[i1].cancelUnLinkCheck[i2]);
                        }
                    }
                }
            }
            loading();
            
            var options = getBASEPOSTAJAX();
            options.url = "/epi/admin/role/" + $('.thisID-input').val() + "/staffs"
            var dataIntArr = [];
            staffIdArr.forEach(function(data,index,arr){  
		        dataIntArr.push(+data);  
		    }); 
            /*options.data.staffIds = [1,2]
            options.data.checkStatus = 0
            options.data.key = ""
            options.data.cancelIds = [1,2] */
            console.log("asd+=" +typeof(dataIntArr) + ""+ typeof([1]))
            options.data.staffIds = dataIntArr
            options.data.key = $('.search-input').val()
            options.data.cancelIds = dataIntArr
            options.data.checkStatus = checkStatus
            $.ajax(options
            	/*{
                url: "/epi/admin/role/" + $('.thisID-input').val() + "/staffs",
                traditional : true,
                data: {
                    staffIds: staffIdArr,
                    checkStatus:checkStatus,//标志是否全选
                    key:$('.search-input').val(),//搜索文字
                    cancelIds:staffIdArr
                },
                type: "POST",
                dataType: 'json',
                success: function(data) {
                	console.log(JSON.stringify(data));
                    loadingHide();
                    var status = data.status;
                    if (status == 500) {
                        setAlert('非法操作！');
                    } else if (status == 400) {
                        setAlert('关联失败！');
                    } else if (status == 200) {
                        setAlert('关联成功');
                    } else {
                        setAlert('系统繁忙，请刷新重试');
                    }
                },
                error: function() {
                    setAlert('系统繁忙，请刷新重试');
                }
            }*/);
        }
        $('.link-modal,.mask').fadeOut();
    })



})

/**
 * [viewPremiss 显示权限]
 */
function viewPremiss() {
    loading();
    $.ajax({
        url: "/epi/admin/permission/permissions",
        type: "GET",
        dataType: 'json',
        success: function(data) {
        	console.log("permissions="+data);
            loadingHide();
            var myTemplate = Handlebars.compile($("#premiss-template").html());
            $('.checkbox-div').html(myTemplate(data));
            fadeModal('.add-modal');
        },
        error: function() {
            setAlert('系统繁忙，请刷新重试');
        }
    });
}

/**
 * [viewTable 显示角色表格]
 * @param  {[int]} pageNum [页码]
 * @param  {[int]} records [每页记录数]
 */
function viewTable(pageNum, records) {
    if (records == undefined) {
        records = 20;
    }
    loading();
    $.ajax({
        url: "/epi/admin/role/roles"+"/"+pageNum+"/"+records,
        type: "GET",
        dataType: 'json',
        success: function(data) {
        	console.log(JSON.stringify(data));
            loadingHide();
            var myTemplate = Handlebars.compile($("#table-template").html());
            // console.log(myTemplate(data));
            $('.role-body').html(myTemplate(data));
        },
        error: function() {
            setAlert('系统繁忙，请刷新重试');
        }
    });
}

/**
 * [viewLinkStaff 查看角色已拥有的员工]
 * @param  {[type]} pageNum [description]
 * @param  {[type]} records [description]
 * @return {[type]}         [description]
 */
function viewLinkStaff(pageNum, records) {
    if (records == undefined) {
        records = 10;
    }
    loading();
    var hdpageNum = 1;
    var hdrecords = 10;
    console.log(pageNum);
    console.log(records);
    console.log()
    pageNum = hdpageNum;
    records = hdrecords;
    
    
    $.ajax({
        url: "/epi/admin/role/"+ $('.thisID-input').val()+ "/" +pageNum+"/"+records ,
        data: 
        {
        	key : $('.search-input').val() 
        },
        type: "POST",
        dataType: 'json',
        success: function(data) {
        	console.log(JSON.stringify(data));
            loadingHide();
            var myTemplate = Handlebars.compile($("#link-staff-template").html());
            console.log(data);
            // console.log(myTemplate(data));
            $('.staff-body').html(myTemplate(data));
        },
        error: function() {
            setAlert('系统繁忙，请刷新重试');
        }
    });
}

/**
 * [viewNotLinkStaff 查询未关联角色的用户]
 * @param  {[type]} pageNum [description]
 * @param  {[type]} records [description]
 * @return {[type]}         [description]
 */
function viewNotLinkStaff(pageNum, records) {
    if (records == undefined) {
        records = 10;
    }
    loading();
    $.ajax({
        url: "/epi/admin/role/" + $('.thisID-input').val() + "/" + pageNum + "/" + records  + "/unrelated",
        type: "POST",
        data:{
        	key : $('.search-input').val()
        },
        dataType: 'json',
        success: function(data) {
        	console.log(JSON.stringify(data));
            loadingHide();
            var myTemplate = Handlebars.compile($("#link-staff-template").html());
            console.log(myTemplate(data));
            console.log(data);
            $('.staff-body').html(myTemplate(data));
        },
        error: function() {
            setAlert('系统繁忙，请刷新重试');
        }
    });
}

/**
 * [nextPage 全选时下一页操作]
 * @param  {[array]} arr [传入的数组，关联或取关]
 */
function allNextPage(arr) {
    var cancelId = [];
    console.log("全选状态：" + checkStatus);
    var unCheck = $('.staff-checked').not('.on_check');
    var flag = 0; //翻页前一页是否存入过数组，0表示未存过
    if (unCheck.length != 0) {
        for (var i = 0; i < unCheck.length; i++) {
            cancelId.push(parseInt(unCheck.eq(i).next('.person-id').val()));
        }
        for (var j = 0; j < arr.length; j++) {
            if (arr[j].pageNum == (parseInt($('.linkPage-input')[0].value) - 1)) { //保存翻页前一页状态
                arr[j].cancelId = cancelId; //翻页时更新取消id数组
                flag = 1; //flag为1表示存过，此时更新
            }
        }
        if (flag == 0) { //flag为0表示未存过，此时新增
            arr.push({
                pageNum: (parseInt($('.linkPage-input')[0].value) - 1),
                cancelId: cancelId
            })
        }
        console.log("所存入翻页前页面和取消ID：")
        console.log(arr);
    }

}

/**
 * [nextPage 全选时上一页操作]
 * @param  {[array]} arr [传入的数组，关联或取关]
 */
function allPrevPage(arr) {
    var cancelId = [];
    console.log("全选状态：" + checkStatus);
    var unCheck = $('.staff-checked').not('.on_check');
    var flag = 0; //该页是否存入过数组，0表示未存过
    if (unCheck.length != 0) {
        for (var i = 0; i < unCheck.length; i++) {
            cancelId.push(parseInt(unCheck.eq(i).next('.person-id').val()));
        }
        for (var j = 0; j < arr.length; j++) {
            if (arr[j].pageNum == (parseInt($('.linkPage-input')[0].value) + 1)) { //
                arr[j].cancelId = cancelId; //翻页时更新取消id数组
                flag = 1; //flag为1表示存过，此时更新
            }
        }
        if (flag == 0) { //flag为0表示未存过，此时新增
            arr.push({
                pageNum: (parseInt($('.linkPage-input')[0].value) + 1),
                cancelId: cancelId
            })
        }
        console.log("所存入翻页前页面和取消ID：")
        console.log(arr);
    }

}

/**
 * [nextPage 非全选时下一页操作]
 */
function nextPage(saveIdArr) {
    var checkId = [];
    console.log("全选状态：" + checkStatus);
    var check = $('.staff-checked.on_check');
    var flag = 0; //翻页前一页是否存入过数组，0表示未存过
    if (check.length != 0) {
        for (var i = 0; i < check.length; i++) {
            checkId.push(parseInt(check.eq(i).next('.person-id').val()));
        }
        for (var j = 0; j < saveIdArr.length; j++) {
            if (saveIdArr[j].pageNum == (parseInt($('.linkPage-input')[0].value) - 1)) { //保存翻页前一页状态
                saveIdArr[j].checkId = checkId; //翻页时更新取消id数组
                flag = 1; //flag为1表示存过，此时更新
            }
        }
        if (flag == 0) { //flag为0表示未存过，此时新增
            saveIdArr.push({
                pageNum: (parseInt($('.linkPage-input')[0].value) - 1),
                checkId: checkId
            })
        }
        console.log("所存入翻页前页面和取消ID：")
        console.log(saveIdArr);
    }


}

/**
 * [nextPage 非全选时上一页操作]
 */
function prevPage(saveIdArr) {
    var checkId = [];
    console.log("全选状态：" + checkStatus);
    var check = $('.staff-checked.on_check');
    var flag = 0; //翻页前一页是否存入过数组，0表示未存过
    if (check.length != 0) {
        for (var i = 0; i < check.length; i++) {
            checkId.push(parseInt(check.eq(i).next('.person-id').val()));
        }
        for (var j = 0; j < saveIdArr.length; j++) {
            if (saveIdArr[j].pageNum == (parseInt($('.linkPage-input')[0].value) + 1)) { //保存翻页前一页状态
                saveIdArr[j].checkId = checkId; //翻页时更新取消id数组
                flag = 1; //flag为1表示存过，此时更新
            }
        }
        if (flag == 0) { //flag为0表示未存过，此时新增
            saveIdArr.push({
                pageNum: (parseInt($('.linkPage-input')[0].value) + 1),
                checkId: checkId
            })
        }
        console.log("所存入翻页前页面和取消ID：")
        console.log(saveIdArr);
    }

}

/**
 * [checkState 每页勾选的状态]
 * @param  {[type]} arr [传入的id对象数组 
 *                      cancelLinkCheck(已关联的取消数组)]
 *                      cancelUnlinkCheck(已关联的取消数组)]
 *                      saveIdArr[非全选时保存的Id数组]
 */
function checkState(arr) {
    console.log(arr);
    var i3; //保存i值（避免document时奇怪的赋值）
    if (checkStatus == 0) {
        for (var j = 0; j < arr.length; j++) {
            if (arr[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //找到当前页码
                for (var i2 = 0; i2 < arr[j].checkId.length; i2++) {
                    for (var i1 = 0; i1 < $('.staff-checked').length; i1++) {
                        // console.log("页面长度：" + $('.staff-checked').length)

                        // console.log("数组长度："+arr[j].checkId.length)
                        // console.log("---------------"+$('.person-id').eq(i1).val());
                        // console.log(arr[j].checkId[0].value)
                        if ($('.person-id').eq(i1).val() == arr[j].checkId[i2]) {
                            i3 = i1;
                            $(document).on('DOMNodeInserted', '.staff-body', function() {
                                    $('.staff-checked').removeClass('on_check');
                                    $('.staff-checked').eq(i3).addClass('on_check');
                                })
                                // debugger;
                        }
                    }
                }
            }
        }
    } else if (checkStatus == 1) {
        for (var j = 0; j < arr.length; j++) {
            if (arr[j].pageNum == parseInt($('.linkPage-input')[0].value)) { //找到当前页码
                for (var i2 = 0; i2 < arr[j].cancelId.length; i2++) {
                    for (var i1 = 0; i1 < $('.staff-checked').length; i1++) {
                        if ($('.person-id').eq(i1).val() == arr[j].cancelId[i2]) {
                            i3 = i1;
                            $(document).on('DOMNodeInserted', '.staff-body', function() {
                                $('.staff-checked').removeClass('on_check');
                                $('.staff-checked').eq(i3).addClass('on_check');
                            })
                        }
                    }
                }
            }
        }
    }
}
